package com.balance.anole.erp.report.action;

import java.util.Date;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import com.balance.anole.common.to.ReportMappingTO;
import com.balance.anole.erp.constant.IIcons;
import com.balance.anole.erp.report.dialog.ReportOrderAnalyzeDialog;
import com.balance.anole.erp.report.view.ReportDemoPreView;
import com.balance.anole.erp.report.view.ReportOrderAnalyzeView;

public class ReportOrderAnalyzeAction extends Action {

	private ReportOrderAnalyzeView view;
	
	private IWorkbenchWindow window;
	
	public void setView(ReportOrderAnalyzeView view) {
		this.view = view;
	}

	public void setWindow(IWorkbenchWindow window) {
		this.window = window;
	}
	
	public ReportOrderAnalyzeAction(ReportOrderAnalyzeView view){
		setView(view);
		setWindow(view.getSite().getWorkbenchWindow());
		//setId("com.balance.anole.erp.report.demo1");
		//setToolTipText("pdf view");
	}
	
	public void setEnabled(boolean enabled){
		super.setEnabled(enabled);
		if(enabled){
			setImageDescriptor(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.REPORT));		
		}else{
			setImageDescriptor(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.DISABLE_REPORT));
		}
	}
	
	public void run(){
		ReportMappingTO selectTO = getSelectReportMappingTO();
		if(selectTO != null){
			String dialogType = selectTO.getReportId();
			
			Shell shell = view.getSite().getShell();
			ReportOrderAnalyzeDialog searchDialog = new ReportOrderAnalyzeDialog(shell,dialogType);
			int digReturn = searchDialog.open();
			
			if(Window.OK == digReturn){
				Date fromDt = searchDialog.getFromDt();
				Date toDt = searchDialog.getToDt();
				
				//System.out.println(fromDt);
				//System.out.println(toDt);
				
				try {
					window.getActivePage().showView(ReportDemoPreView.ID,dialogType,IWorkbenchPage.VIEW_ACTIVATE);
				} catch (PartInitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			
			
		}
	}
	
	private ReportMappingTO getSelectReportMappingTO(){
		ISelection s = view.getSite().getWorkbenchWindow().getSelectionService().getSelection(ReportOrderAnalyzeView.ID);
		if(s instanceof StructuredSelection){
			StructuredSelection ss = (StructuredSelection)s;
			Object o = ss.getFirstElement();
			if(o instanceof ReportMappingTO){
				return (ReportMappingTO)o;
			}
		}
		return null;
	}

}
